package com.haighcom.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.haighcom.entity.PageResult;
import com.haighcom.mapper.CheckItemMapper;
import com.haighcom.model.CheckItem;
import com.haighcom.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CheckItemServiceImpl implements CheckItemService {
    @Autowired
    private CheckItemMapper checkItemMapper;
    @Override
    public void add(CheckItem checkItem) {
        checkItemMapper.add(checkItem);
    }

    @Override
    public PageResult queryPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);//开启分页，传入当前页码和页面大小，会自动为下一条sql语句添加分页效果
        Page<CheckItem> page = checkItemMapper.selectByCondition(queryString);
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    public void delete(Integer id) {
        //检查当前检查项是否和检查组关联
        long row = checkItemMapper.findCountByCheckItemId(id);
        if(row > 0){
            //如果有关联就说明已经被用户购买，不能删除
            throw new RuntimeException("当前检查项因为被引用，无法删除");
        }
        checkItemMapper.deleteById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkItemMapper.edit(checkItem);
    }

    @Override
    public CheckItem findById(Integer id) {
        return checkItemMapper.findById(id);
    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> checkItemList = checkItemMapper.findAll();
        return checkItemList;
    }
}
